<template>
	<view class="main">
		<mx-navbar :title="'我的订单'"></mx-navbar>
		<view class="nav-box">
			<scroll-view scroll-x class="bg-white nav text-center" scroll-with-animation :scroll-left="scrollLeft">
				<view class="cu-item" :class="index == navCurr ? 'act-color' : ''" v-for="(item, index) in navs"
					:key="index" @tap="navSelect" :data-id="index">
					{{ item.name }}
					<view class="act-line" v-if="index == navCurr"></view>
				</view>
			</scroll-view>
		</view>

		<view class="body">
			<view class="ul" v-if="order_list && order_list.length > 0">
				<view class="li" v-for="(item, index) in order_list" :key="index" @click="goDetail(item)">

					<view class="status">
						<view>
							<text class="time">订单号：{{ item.order_sn }}</text>
						</view>
						<text class="status-type">
							{{ item.state_name }}
						</text>
					</view>
					<view class="detail">
						<view class="imglist">
							<view class="item" :key="gindex" v-for="(product, gindex) in item.goods_list">
								<image class="img-item" :src="$imgUrl + product.image" :key="index"></image>
								<view class="item-text">
									<view class="name">{{ product.name }}</view>
									<view class="spec text-limit-2">
										<text v-if="product.mdl">已选：{{ product.mdl }}</text>
									</view>
									<view class="num">
										<view class="order-price priceColor">
											<text class="n-price priceColor">￥</text>{{ product.original_price }}
										</view>
										<view>× {{ product.count }}</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="operate-box">
						<view class="operate">
							<view class="time">{{ item.create_time }}</view>
							<view class="product-details">共 <text class="pn mprice">{{ item.order_goods_count }}</text>
								件
								实付：<text class="pi mprice">¥</text><text class="pn mprice">{{ item.payment_amount
									}}</text>
							</view>
						</view>
						<view class="operate justify-end ">
							<view class="operate-but" v-if="item.state == 1">
								<view class="cu-btn opt-btn cancel round" @click.stop="orderCancel(item)">
									取消订单
								</view>
								<view class="cu-btn opt-btn round" @click.stop="orderPay(item)">
									立即支付
								</view>
							</view>
							<view class="operate-but" v-if="item.state == 3">
								<view class="cu-btn opt-btn round" @click.stop="receive(item)">
									确认收货
								</view>
							</view>
						</view>
					</view>

				</view>
			</view>

			<view class="empty" v-else>
				<mx-empty txt="暂无记录～"></mx-empty>
			</view>
		</view>

		<mx-loading v-model="load" v-if="order_list && order_list.length > 0"></mx-loading>

	</view>
</template>

<script setup>
const {
	proxy
} = getCurrentInstance()
const data = reactive({
	navCurr: 0,
	scrollLeft: 0,
	navs: [{
		name: '全部',
		index: 0
	},
	{
		name: '待付款',
		index: 1
	},
	{
		name: '待发货',
		index: 2
	},
	{
		name: '待收货',
		index: 3
	},
	{
		name: '已收货',
		index: 4
	}
	]
})
const {
	navCurr,
	scrollLeft,
	navs
} = toRefs(data)


onLoad((options) => {
	data.navCurr = options.state || 0
})

onShow((options) => {
	getOrderList(true)
})

const order_list = ref([])
const pn = ref(1)
const load = ref('loading')
async function getOrderList(loading) {
	let res = await proxy.$api('OrderList', {
		state: data.navCurr == 0 ? '' : data.navCurr,
		page: pn.value,
		page_size: 10
	}, false)
	if (res.code === 0) {
		if (pn.value == 1) {
			order_list.value = res.data.list || []
		} else {
			order_list.value = order_list.value.concat(res.data.list || [])
		}
		if (res.data.list.length == 0 || res.data.list.length < 10) {
			load.value = 'finish'
		}
		if (res.data.list.length == 0 && pn.value != 1) {
			pn.value -= 1
		}
	}
}

function navSelect(e) {
	pn.value = 1
	load.value = 'loading'
	data.navCurr = e.currentTarget.dataset.id;
	data.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
	getOrderList()
}

// 取消订单
async function orderCancel(item) {
	let res = await proxy.$api('OrderCancel', { order_sn: item.order_sn })
	if (res.code === 0) {
		item.state = 5
		item.state_name = '已取消'
	}
}

// 确认收货
async function receive(item) {
	let res = await proxy.$api('OrderReceive', { order_sn: item.order_sn })
	if (res.code === 0) {
		item.state = 4
		item.state_name = '已收货'
	}
}

// 支付
async function orderPay(item) {
	let res = await proxy.$api("OrderPay", {
		order_sn: item.order_sn
	});
	if (res.code === 0) {
		proxy.$tools.pay(res.data).then(async _ => {
			proxy.$go(`/pages/pay/res?sn=${item.order_sn}&type=succ`)
		}).catch(_ => {
			proxy.$go(`/pages/pay/res?sn=${item.order_sn}&type=error`)
		})
	} else {
		proxy.$go(`/pages/pay/res?sn=${item.order_sn}&type=error`)
	}
}

// 跳转商品详情
function goDetail(item) {
	proxy.$go(`/pages-order/detail/detail?sn=${item.order_sn}`)
}

onReachBottom(() => {
	if (load.value != 'finish') {
		pn.value += 1
		getOrderList()
	}
})

</script>

<style lang="scss" scoped>
.main {
	position: relative;
}

.nav-box {
	width: 100%;
	position: fixed;
	z-index: 3;

	.cu-item {
		font-size: 26rpx;
		position: relative;

		.act-line {
			width: 40rpx;
			height: 6rpx;
			border-radius: 6rpx;
			background-color: $main_color;
			position: absolute;
			bottom: 0;
			left: 50%;
			margin-left: -20rpx;
			z-index: 3;
		}
	}

	.act-color {
		color: $main_color;
		font-weight: 500;
		font-size: 28rpx;

	}
}

.body {
	padding: 20rpx;
	padding-top: 110rpx;
	position: relative;

	.ul {
		width: 100%;
		height: auto;

		.li {
			background-color: #fff;
			overflow: hidden;
			margin-bottom: 24rpx;
			padding: 0 36rpx 0;

			.status {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 38rpx 0 24rpx;
				border-bottom: 1px solid #f0f1f3;

				.time {
					color: #000;
					font-size: 24rpx;
				}

				.status-type {
					color: #f00;
					font-size: 24rpx;
				}

				.status-succ {
					color: green;
					font-size: 24rpx;
				}
			}

			.detail {
				padding: 0px;
				display: flex;

				.imglist {
					flex-grow: 1;
					padding: 24rpx 0px;
					overflow: hidden;

					.img-item {
						width: 176rpx;
						height: 176rpx;
						margin-right: 16rpx;
						flex-grow: 0;
					}

					.item {
						display: flex;
						width: 100%;
						border-bottom: 1px solid #f0f1f3;
						padding-bottom: 24rpx;
						margin-bottom: 24rpx;
						position: relative;

						&:nth-last-of-type(1) {
							margin-bottom: 0;
						}

						.item-text {
							flex: 1;
							position: relative;

							.name {
								width: 100%;
								display: -webkit-box;
								overflow: hidden;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
								text-overflow: ellipsis;
								color: #1f1f1f;
								font-size: 24rpx;
								line-height: 36rpx;
								margin-bottom: 10rpx;
								padding: 2rpx 0;
							}

							.spec {
								width: 100%;
								color: #999999;
								font-size: 22rpx;
								position: relative;
								margin-bottom: 10rpx;
							}

							.num {
								display: flex;
								justify-content: space-between;
								flex: 1;
								font-size: 24rpx;
								position: absolute;
								width: 100%;
								bottom: 10rpx;
								left: 0;

								.order-price {
									font-size: 28rpx;

									.n-price {
										font-size: 20rpx;
									}
								}
							}
						}
					}
				}


			}


			.operate {
				display: flex;
				align-items: center;

				.time {
					flex: 1;
					font-size: 24rpx;
					color: #999;
					padding-bottom: 24rpx;
				}

				.product-details {
					padding: 0 0 24rpx;
					text-align: right;
					font-size: 26rpx;
					color: #666;

					.pi {
						font-size: 20rpx;
						color: #000;
					}

					.pn {
						font-size: 30rpx;
						color: #000;
					}
				}

				.operate-but {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					// padding-bottom: 12rpx;

					.opt-btn {
						margin-left: 16rpx;
						background: $main_color;
						line-height: 64rpx;
						// border-radius: 8rpx;
						border: 1px solid #ececec;
						line-height: 64rpx;
						font-size: 24rpx;
						padding: 0 30rpx;
						color: #fff;
						margin-bottom: 24rpx;

						&.cancel {
							background: none;
							color: #1f1f1f;
						}
					}
				}
			}
		}
	}
}

.empty {
	width: 100%;
	height: 80vh;
}
</style>